<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="爱洞ID" prop="userId">
        <el-input v-model="queryParams.platformId" placeholder="请输入用户ID" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="提现状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择提现状态 " clearable>
          <el-option v-for="dict in dict.type.app_check_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式 " prop="status">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式 " clearable>
          <el-option v-for="dict in dict.type.withdraw_type" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="付款状态 " prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择付款状态 " clearable>
          <el-option v-for="dict in dict.type.app_pay_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="提现时间" prop="withdrawDate">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="createTime" type="datetimerange" align="right"
                        unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                        :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核日期" prop="checkDate">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="checkTime" type="datetimerange" align="right"
                        unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                        :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:payWithdraw:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payWithdrawList" :summary-method="getSummaries" show-summary>

      <el-table-column label="更多" type="expand" width="50">
        <template slot-scope="scope">
          <userInfo :userId="scope.row.userId" :userName="scope.row.phone"></userInfo>
        </template>
      </el-table-column>
      <el-table-column label="爱动ID" align="center" prop="platformId">
        <template slot-scope="scope">
          <a v-if="scope.row.sex === 1" style="color: #FF6EC7" @click="toUserInfo(scope.row.platformId)">{{ scope.row.platformId }}
          </a>
          <a v-else style="color: #7099F9" @click="toUserInfo(scope.row.platformId)">{{ scope.row.platformId }}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="phone"/>
      <el-table-column label="支付方式 " align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.withdraw_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="提现钻石" align="center" prop="diamondNum"/>
      <el-table-column label="手续费" align="center" prop="phone">
        <template slot-scope="scope">
          {{ scope.row.rate }}%
        </template>
      </el-table-column>
      <el-table-column label="预计到账金额" align="center" prop="expectedNum"/>
      <el-table-column label="收款账号" align="center" prop="withdrawAccount"/>
      <el-table-column label="收款人" align="center" prop="accountName"/>
      <el-table-column label="审核状态 " align="center" prop="checkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="放款状态 " align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="放款结果" align="center" prop="payResult">
        <template slot-scope="scope">
          <span v-if="scope.row.payResult">{{ JSON.parse(scope.row.payResult).tradeResultDescribe }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="发起时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="checkTime" width="180">

      </el-table-column>
      <el-table-column label="放款交易号" align="center" prop="outerTradeNo"/>
      <el-table-column label="审核人" align="center" prop="updateBy"/>
      <el-table-column label="操作备注" align="center" prop="remark"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.checkStatus!=1" size="mini" type="text" icon="el-icon-check"
                     @click="handlePass(scope.row)" v-hasPermi="['system:payWithdraw:pass']">审核通过
          </el-button>
          <el-button :disabled="scope.row.checkStatus!=1" size="mini" type="text" icon="el-icon-close"
                     @click="handleRefuse(scope.row)" v-hasPermi="['system:payWithdraw:refuse']">审核拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>


    <!-- 通过审核 -->
    <el-dialog title="通过审核" :visible.sync="openPass" width="500px" append-to-body>
      <el-form ref="form" :model="bo" :rules="rules" label-width="80px">
        <el-form-item label="操作备注" prop="reason">
          <el-input v-model="bo.reason" placeholder="请输入操作备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="toPass">确 定</el-button>
        <el-button @click="openPass=false">取 消</el-button>
      </div>
    </el-dialog>


    <!--拒绝审核 -->
    <el-dialog title="拒绝审核" :visible.sync="openRefuse" width="500px" append-to-body>
      <el-form ref="form" :model="bo" :rules="rules" label-width="80px">
        <el-form-item label="操作备注" prop="reason">
          <el-input v-model="bo.reason" placeholder="请输入操作备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="toRefuse">确 定</el-button>
        <el-button @click="openRefuse=false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listPayWithdraw, pass, refuse} from "@/api/service/pay/payWithdraw";
import userInfo from "@/views/service/appUser/userInfo.vue"

export default {
  name: "PayWithdraw",
  dicts: ['app_check_status', 'withdraw_type', 'app_pay_status'],
  components: {
    userInfo
  },
  data() {
    return {
      showSearch: true,
      openRefuse: false,
      openPass: false,
      bo: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付-提现表格数据
      payWithdrawList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      checkTime: [],
      createTime: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        platformId: undefined,
        status: '1',
        payType: undefined,

        checkDateBefore: undefined,
        checkDateAfter: undefined,
        withdrawDateBefore: undefined,
        withdrawDateAfter: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getSummaries(param) {

      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {

        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        if (index !== 6) {
          sums[index] = '/';
          return;
        }
        const values = data.map(item => {
          return Number(item[column.property]) * 100
          // if (item.checkStatus === 2 && item.payStatus === 2) {
          //   return Number(item[column.property]) * 100
          // } else {
          //   return 0;
          // }
        });
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0) / 100;
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    },
    toPass() {
      pass(this.bo).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.openPass = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    toRefuse() {
      refuse(this.bo).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.openRefuse = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 查询支付-提现列表 */
    getList() {

      if (this.checkTime) {
        this.queryParams.checkDateBefore = this.checkTime[0]
        this.queryParams.checkDateAfter = this.checkTime[1]
      }
      if (this.createTime) {
        this.queryParams.withdrawDateBefore = this.createTime[0]
        this.queryParams.withdrawDateAfter = this.createTime[1]
      }

      this.loading = true;
      listPayWithdraw(this.queryParams).then(response => {
        this.payWithdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        userId: undefined,
        num: undefined,
        status: undefined,
        withdrawDate: undefined,
        snap: undefined,
        payType: undefined,
        checkDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付-提现";
    },
    /** 审核通过 */
    handlePass(row) {
      this.bo.id = row.id
      this.openPass = true;
    },

    /** 删除按钮操作 */
    handleRefuse(row) {
      this.bo.id = row.id
      this.openRefuse = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/payWithdraw/export', {
        ...this.queryParams
      }, `payWithdraw_${new Date().getTime()}.xlsx`)
    },
    /**
     * 点击用户爱动id跳转到用户页面
     * @param platformId
     */
    toUserInfo(platformId) {
      this.$router.push({path: "/user/appUser", query: {id: platformId}});
    }
  }
};
</script>

<style lang="saas">

</style>
